package com.ruoyi.kyjData.controller;

import java.util.List;
import java.util.concurrent.ExecutionException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.kyjData.domain.pad.DataKyjModifyReqDTO;
import com.ruoyi.kyjData.domain.pad.KyjTaskResqDTO;
import com.ruoyi.kyjData.domain.plat.DataKyj;
import com.ruoyi.kyjData.service.IDataKyjService;
import com.ruoyi.task.domain.entity.DataTaskEntity;
import com.ruoyi.task.service.IDataTaskService;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 空压机测试结果Controller
 * 
 * @author ruoyi
 * @date 2023-11-16
 */
@RestController
@RequestMapping("/kyjData/all")
@Api(value="空压机测试",tags = "空压机测试接口")
public class DataKyjController extends BaseController
{
    @Autowired
    private IDataKyjService dataKyjService;

    @Autowired
    private IDataTaskService dataTaskService;

    /**
     * 查询空压机测试结果列表
     */
    @PreAuthorize("@ss.hasPermi('kyjData:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(DataKyj dataKyj)
    {
        startPage();
        List<DataKyj> list = dataKyjService.selectDataKyjList(dataKyj);
        return getDataTable(list);
    }

    /**
     * 导出空压机测试结果列表
     */
    @PreAuthorize("@ss.hasPermi('kyjData:task:export')")
    @Log(title = "空压机测试结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DataKyj dataKyj)
    {
        List<DataKyj> list = dataKyjService.selectDataKyjList(dataKyj);
        ExcelUtil<DataKyj> util = new ExcelUtil<>(DataKyj.class);
        util.exportExcel(response, list, "空压机测试结果数据");
    }

    /**
     * 获取空压机测试结果详细信息
     */
    @PreAuthorize("@ss.hasPermi('kyjData:task:query')")
    @GetMapping(value = "/{kyjId}")
    public AjaxResult getInfo(@PathVariable("kyjId") Long kyjId)
    {
        return success(dataKyjService.selectDataKyjByKyjId(kyjId));
    }

    /**
     * 获取空压机测试结果详细信息
     */
    @PreAuthorize("@ss.hasPermi('kyjData:task:query')")
    @GetMapping(value = "/getInfoByDataKyjTaskId/{dataTaskId}")
    @ApiImplicitParam(paramType="query", name = "dataTaskId", value = "任务id", required = true, dataType = "Long")
    @ApiOperation(value="获取空压机测试结果详细信息")
    public AjaxResult getInfoByDataKyjTaskId(@PathVariable("dataTaskId") Long dataTaskId) throws ExecutionException, InterruptedException {
        return success(dataKyjService.selectKyjTaskByDataTaskId(dataTaskId));
    }

    /**
     * 新增空压机测试结果
     */
    @PreAuthorize("@ss.hasPermi('kyjData:task:add')")
    @Log(title = "空压机测试结果", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DataKyj dataKyj)
    {
        return toAjax(dataKyjService.insertDataKyj(dataKyj));
    }

    /**
     * 空压机参数调整
     */
    @PreAuthorize("@ss.hasPermi('kyjData:task:changeparam')")
    @Log(title = "空压机参数调整", businessType = BusinessType.INSERT)
    @PostMapping("/changeKyjParam")
    @ApiOperation(value="空压机参数调整")
    public AjaxResult changeKyjParam(@ApiParam(name="空压机参数调整",value="传入json格式",required=true)@RequestBody DataKyjModifyReqDTO dataKyjModifyReqDTO) {
        return AjaxResult.success(dataKyjService.modify(dataKyjModifyReqDTO));
    }

    /**
     * 修改空压机测试结果
     */
    @PreAuthorize("@ss.hasPermi('kyjData:task:edit')")
    @Log(title = "空压机测试结果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DataKyj dataKyj)
    {
        return toAjax(dataKyjService.updateDataKyj(dataKyj));
    }

    /**
     * 更新空压机数据
     *
     * @param kyjTaskResqDTO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('platform:task:edit')")
    @PostMapping("/task/update")
    public AjaxResult updateDetail(@RequestBody KyjTaskResqDTO kyjTaskResqDTO) {
        DataTaskEntity dataTaskEntity = kyjTaskResqDTO.getDataTaskEntity();
        List<DataKyj> resultList = kyjTaskResqDTO.getResultList();
        dataTaskService.updateDataTask(dataTaskEntity);
        dataKyjService.updateBatch(resultList);
        return AjaxResult.success();
    }

    /**
     * 根据任务id删除空压机测试结果
     */
    @PreAuthorize("@ss.hasPermi('kyjData:task:remove')")
    @Log(title = "根据任务id删除空压机测试结果", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{dataTaskId}/{kyjId}")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "dataTaskId", value = "任务id", required = true, dataType = "Long"),
            @ApiImplicitParam(paramType="query", name = "kyjId", value = "空压机id", required = true, dataType = "Long"),
    })
    @ApiOperation(value="根据任务id删除空压机测试结果")
    public AjaxResult remove(@PathVariable("dataTaskId")Long dataTaskId, @PathVariable("kyjId")Long kyjId)
    {
        return toAjax(dataKyjService.updateDataKyjByHjId(dataTaskId, kyjId));
    }

    /**
     * 根据任务id删除整条空压机测试结果
     */
    @PreAuthorize("@ss.hasPermi('kyjData:task:remove')")
    @Log(title = "根据任务id删除整条空压机测试结果", businessType = BusinessType.DELETE)
    @DeleteMapping("/updateDataKyjByDataTaskId/{dataTaskId}")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "dataTaskId", value = "任务id", required = true, dataType = "Long"),
    })
    @ApiOperation(value="根据任务id删除整条空压机测试结果")
    public AjaxResult updateDataKyjByDataTaskId(@PathVariable("dataTaskId")Long dataTaskId)
    {
        return toAjax(dataKyjService.updateDataKyjByDataTaskId(dataTaskId));
    }

    /**
     * 删除空压机测试结果
     */
    @PreAuthorize("@ss.hasPermi('kyjData:task:remove')")
    @Log(title = "空压机测试结果", businessType = BusinessType.DELETE)
	@DeleteMapping("/{kyjIds}")
    public AjaxResult remove(@PathVariable Long[] kyjIds)
    {
        return toAjax(dataKyjService.deleteDataKyjByKyjIds(kyjIds));
    }

    /**
     * 保存的空压机数据导入报告
     */
    @PreAuthorize("@ss.hasPermi('kyjData:import:query')")
    @PostMapping("/dataKyjImportReport")
    @ApiOperation(value="保存的空压机数据导入报告")
    public AjaxResult dataKyjImportReport(@ApiParam(name="测试项目结果数据",value="传入json格式",required=true)@RequestBody DataKyj dataKyj){
        return AjaxResult.success(dataKyjService.dataKyjImportReport(dataKyj));
    }
}
